package com.anlu.rabbitmq.exchange.direct;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * 生产者
 */
public class DirectProduct {
    public final static String EXCHANGE_NAME = "direct_logs";

    public static void main(String[] args)throws Exception{
        /* 创建连接,连接到RabbitMQ*/
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        Connection connection = connectionFactory.newConnection();

        //创建信道
        Channel channel =connection.createChannel();
        //创建交换器
        channel.exchangeDeclare(EXCHANGE_NAME,"direct");
        /*日志消息级别，作为路由键使用*/
        String[]serverties={"error","info","warning"};

        for(int i=0;i<3;i++){
            String severity=serverties[i];
            String msg = "Hellol,RabbitMq"+(i+1);
            /*发布消息，需要参数：交换器，路由键，其中以日志消息级别为路由键*/
            channel.basicPublish(EXCHANGE_NAME,severity,null,
                    msg.getBytes());
            System.out.println("Sent "+severity+":"+msg);
        }
        channel.close();
        connection.close();

    }


}
